package com.xci.sys.api;

import com.xci.core.annotation.*;
import com.xci.core.base.CodeValue;
import com.xci.core.internal.Const;
import com.xci.core.base.RestMessage;
import com.xci.core.helper.ExcelHelper;
import com.xci.core.base.ApiController;
import com.xci.sys.entity.SysParam;
import com.xci.sys.filter.ParamFilter;
import com.xci.sys.service.ParamService;
import io.swagger.annotations.*;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 参数接口
 * @author 吕艳阳
 */
@ApiSort(7)
@Api(tags = "参数接口")
@Authorize
@EncryptResponseBody
@DecryptRequestBody
@RestController
@RequestMapping(value = "/api/sys/param", produces = Const.P_JSON)
public class ParamApiController extends ApiController {
    @Resource
    private ParamService paramService;//参数服务

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "新建参数")
    @Authorize(code = "sys.param.insert")
    @PostMapping(value = "/insert")
    public RestMessage insert(@RequestBody SysParam entity) {
        return paramService.insert(entity);
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "修改参数")
    @Authorize(code = "sys.param.update")
    @PostMapping(value = "/update")
    public RestMessage update(@RequestBody SysParam entity) {
        return paramService.update(entity);
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "批量更新参数键值对")
    @PostMapping(value = "/batchUpdate")
    public RestMessage batchUpdate(@RequestBody List<CodeValue> pairs) {
        return paramService.batchUpdate(pairs);
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "批量保存(新建或者更新)参数对象")
    @PostMapping(value = "/batchSave")
    public RestMessage batchSave(@RequestBody List<SysParam> entities) {
        return paramService.batchSave(entities);
    }

    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "删除参数")
    @ApiImplicitParam(name = "ids", value = "参数主键字符串", required = true)
    @Authorize(code = "sys.param.delete")
    @PostMapping(value = "/delete")
    public RestMessage delete(String ids) {
        return paramService.delete(ids);
    }

    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "检查参数编码是否存在")
    @ApiImplicitParam(name = "code", value = "参数编码", required = true)
    @PostMapping(value = "/existByCode/{code}")
    public RestMessage existByCode(@PathVariable String code) {
        return RestMessage.result(paramService.existByCode(code));
    }

    @ApiOperation(value = "根据主键查询单个参数")
    @ApiOperationSupport(order = 7)
    @ApiImplicitParam(name = "id", value = "参数主键", required = true)
    @PostMapping(value = "/selectById/{id}")
    public RestMessage<SysParam> selectById(@PathVariable String id) {
        return RestMessage.success(paramService.selectById(id));
    }

    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "根据编码查询单个参数")
    @ApiImplicitParam(name = "code", value = "参数编码", required = true)
    @PostMapping(value = "/selectByCode/{code}")
    public RestMessage<SysParam> selectByCode(@PathVariable String code) {
        return RestMessage.success(paramService.selectByCode(code));
    }

    @ApiOperationSupport(order = 9)
    @ApiOperation(value = "根据编码查询单个参数值")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "参数编码", required = true),
            @ApiImplicitParam(name = "defaultValue", value = "如果没有找到指定编码的参数,则返回此默认值", required = true)
    })
    @PostMapping(value = "/selectValueByCode")
    public RestMessage<String> selectValueByCode(String code, String defaultValue) {
        return RestMessage.success(paramService.selectValueByCode(code, defaultValue));
    }

    @ApiOperationSupport(order = 10)
    @ApiOperation(value = "查询参数键值对列表")
    @Authorize(code = "sys.param.select")
    @PostMapping(value = "/selectPairList")
    public RestMessage<List<CodeValue>> selectPairList() {
        return RestMessage.success(paramService.selectPairList());
    }

    @ApiOperationSupport(order = 11)
    @ApiOperation(value = "查询参数列表")
    @Authorize(code = "sys.param.select")
    @PostMapping(value = "/selectList")
    public RestMessage<List<SysParam>> selectList(ParamFilter filter) {
        return RestMessage.success(paramService.selectList(filter));
    }

    @ApiOperationSupport(order = 12)
    @ApiOperation(value = "导出参数列表")
    @ApiImplicitParam(name = "fieldNames", value = "导出字段名字符串")
    @PostMapping(value = "/export", produces = {Const.P_OCTET, Const.P_JSON})
    public void export(ParamFilter filter, @RequestParam(required = false) String fieldNames) {
        ExcelHelper.exportWeb(paramService.selectList(filter), SysParam.class, "系统参数列表", fieldNames);
    }

    @ApiOperationSupport(order = 13)
    @ApiOperation(value = "刷新参数缓存")
    @Authorize(code = "sys.param.refreshCache")
    @PostMapping(value = "/refreshCache")
    public RestMessage refreshCache() {
        paramService.refreshCache();
        return RestMessage.success();
    }
}